﻿using UnityEngine;
using System.Collections;
using System.Security.Cryptography;    
using    System.IO;   
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System;
public class MD5JiaMi : MonoBehaviour {

	private bool interactionInited=false;
	public static MD5JiaMi initMD5;

	/// <summary>
	/// MD5加密
	/// </summary>
	/// <param name="realMsg">需加密的明文</param>
	/// <param name="key">加密标准算法的机密密钥</param>
	/// <param name="iv">对称算法的初始化向量</param>
	/// <returns>加密后密码的16进制表示</returns>
	public static string MD5Encrypt(string realMsg, string key, string iv)
	{
		string sKey = key.PadLeft(8, '0');
		string sIV = iv.PadLeft(8, '0');
		DESCryptoServiceProvider des = new DESCryptoServiceProvider();
		byte[] inputByteArray = Encoding.Default.GetBytes(realMsg);
		des.Key = ASCIIEncoding.ASCII.GetBytes(sKey.Substring(0, 8));
		des.IV = ASCIIEncoding.ASCII.GetBytes(sIV.Substring(0, 8));
		MemoryStream ms = new MemoryStream();
		CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
		cs.Write(inputByteArray, 0, inputByteArray.Length);
		try
		{
			cs.FlushFinalBlock();
		}
		catch //(Exception err)
		{
			return realMsg;
		}
		StringBuilder ret = new StringBuilder();
		foreach (byte b in ms.ToArray())
		{
			ret.AppendFormat("{0:X2}", b);
		}
		ret.ToString();
		return ret.ToString();
	}
	// <summary>
	/// MD5解密,对MD5Encrypt的加密内容进行解密
	/// </summary>
	/// <param name="encryptedMsg">已经加密的密码密文</param>
	/// <param name="key">加密标准算法的机密密钥</param>
	/// <param name="iv">对称算法的初始化向量</param>
	/// <returns></returns>
	public static string MD5Decrypt(string encryptedMsg, string key, string iv)
	{
		string sKey = key.PadLeft(8, '0');
		string sVI = iv.PadLeft(8, '0');
		DESCryptoServiceProvider des = new DESCryptoServiceProvider();
		byte[] inputByteArray = new byte[encryptedMsg.Length / 2];
		for (int x = 0; x < encryptedMsg.Length / 2; x++)
		{
			int i = Convert.ToInt32(encryptedMsg.Substring(x * 2, 2), 16);
			inputByteArray[x] = (byte)i;
		}
		des.Key = ASCIIEncoding.ASCII.GetBytes(sKey.Substring(0, 8));
		des.IV = ASCIIEncoding.ASCII.GetBytes(sVI.Substring(0, 8));
		MemoryStream ms = new MemoryStream();
		CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
		cs.Write(inputByteArray, 0, inputByteArray.Length);
		try
		{
			cs.FlushFinalBlock();
		}
		catch//(Exception err)
		{
			Debug.Log("MD5 error");
			return encryptedMsg;
		}
		//StringBuilder ret = new StringBuilder();
		return System.Text.Encoding.Default.GetString(ms.ToArray());
	}
	
	public string GetMD5(string input) 
	{ 
		System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); 
		byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); 
		byte[] hash = md5.ComputeHash(inputBytes); 
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < hash.Length; i++)
		{ sb.Append(hash[i].ToString("X2"));
		}
		return sb.ToString();
	}
	// Use this for initialization
	void Start () {
//		string str, str2;
//		str = MD5Encrypt ("13800138000AB1234121212121212u2TiiBQaDF4C", "u2TiiBQaDF4C","aa");
//		Debug.Log ("Md5加密后：" + str);
//		Debug.Log("Md5解码后："+MD5Decrypt(str,"u2TiiBQaDF4C","aa"));
//		Debug.Log ("13800138000AB1234121212121212解码：" + MD5Decrypt ("13800138000AB1234121212121212", "u2TiiBQaDF4C", "aa"));
////		string a="13800138000AB1234121212121212u2TiiBQaDF4C"; //加密前数据
////		string b; //加密后数据
////		b = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (a, "MD5");
////			Debug.Log(b);
		//		Debug.Log ("GetMD5="+GetMD5 ("13800138000AB1234121212121212u2TiiBQaDF4C"));
		initMD5 = this;
		interactionInited = true;

//	Debug.Log ("md5: "+GetMD5("18665057471111265857884120,57471checku2TiiBQaDF4C"));
//		string str = ReadFileTool.AddressToClass<FormMessage> ("data/FormData").order;
//		Debug.Log("----------------Json="+str);
//		Debug.Log("++++++++++++++++Json="+ReadFileTool.JsonToClass<FormMessage>(str).)
	}
	void OnApplicationQuit()
	{
		// uninitialize Kinect interaction
		if(interactionInited)
		{
			interactionInited = false;
			initMD5 = null;
		}
	}

}
